import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.io.IOException;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Feb 26, 2007
 * Time: 12:28:32 PM
 * Revision: 0
 */
public class SHZPCanvas extends Canvas implements Runnable
{
    public static Graphics g;
    public static int gameState;
    public boolean newState;
    public long timer;


    //
    private Image img_logo;
    private Image img_splash;


    public SHZPCanvas()
    {
        switchState(STATE.INIT);

        (new Thread(this)).start();
        setFullScreenMode(true);
    }

    public void updateGame(Graphics graphics)
    {
        g = graphics;
        int oldState = gameState;

        switch (gameState)
        {
            case STATE.INIT:
                state_INIT(newState);
                break;

            case STATE.LOGO:
                state_LOGO(newState);
                break;

            case STATE.COPYRIGHT:
                state_COPYRIGHT(newState);
                break;

            case STATE.LOADING:
                state_LOADING(newState);
                break;

            case STATE.SPLASH:
                state_SPLASH(newState);
                break;

            case STATE.MAIN_MENU:
                state_MAIN_MENU(newState);
                break;

            case STATE.INSTANT_PLAY:
                state_INSTANT_PLAY(newState);
                break;

            case STATE.PLAY:
                state_PLAY(newState);
                break;

            case STATE.OPTIONS:
                state_OPTIONS(newState);
                break;

            case STATE.HELP:
                state_HELP(newState);
                break;

            case STATE.ABOUT:
                state_ABOUT(newState);
                break;
        }
        if (oldState == gameState)
            newState = false;
    }

    public void destroy()
    {
    }

    protected void paint(Graphics graphics)
    {
        updateGame(graphics);
    }

    public void run()
    {
        long t1, t2, maxDif;

        maxDif = 1000 / DEF.FPS;

        while (gameState != STATE.EXIT)
        {
            t1 = System.currentTimeMillis();
            repaint();
            serviceRepaints();
            t2 = System.currentTimeMillis();
            t2 = maxDif - (t2 - t1);

            if (t2 < 10)
                t2 = 10;
            if (t2 > maxDif)
                t2 = maxDif;

            try
            {
                Thread.sleep(t2);
            }
            catch (InterruptedException e)
            {
                processException(e);
            }
        }
    }

    public void processException(Exception e)
    {
        e.printStackTrace();
    }

    //------------------------------------------
    // STATE SPECIFICS
    //------------------------------------------

    public void switchState(int state)
    {
        gameState = state;
        newState = true;
    }

    public void state_INIT(boolean init)
    {
        g.setColor(0x000000);
        g.fillRect(0, 0, DEF.SCREEN_WIDTH, DEF.SCREEN_HEIGHT);

        try
        {
            img_logo = Image.createImage("/logo.png");
        }
        catch (IOException e)
        {
            processException(e);
        }

        switchState(STATE.LOGO);
    }

    public void state_LOGO(boolean init)
    {
        g.setColor(0x000000);
        g.fillRect(0, 0, DEF.SCREEN_WIDTH, DEF.SCREEN_HEIGHT);
        g.drawImage(img_logo, DEF.SCREEN_WIDTH / 2, DEF.SCREEN_HEIGHT / 2, Graphics.HCENTER | Graphics.VCENTER);

        if (init)
            timer = System.currentTimeMillis();

        if (System.currentTimeMillis() - timer > 3000)
            switchState(STATE.COPYRIGHT);
    }

    public void state_COPYRIGHT(boolean init)
    {
        g.setColor(0x000000);
        g.fillRect(0, 0, DEF.SCREEN_WIDTH, DEF.SCREEN_HEIGHT);
        img_logo = null;
        switchState(STATE.LOADING);
    }

    public void state_LOADING(boolean init)
    {
        try
        {
            img_splash = Image.createImage("/splash.png");
        }
        catch (IOException e)
        {
            processException(e);
        }
        switchState(STATE.SPLASH);
    }

    public void state_SPLASH(boolean init)
    {
        g.drawImage(img_splash, 0, 0, Graphics.LEFT | Graphics.TOP);
    }

    public void state_MAIN_MENU(boolean init)
    {
    }

    public void state_INSTANT_PLAY(boolean init)
    {
    }

    public void state_PLAY(boolean init)
    {
    }

    public void state_OPTIONS(boolean init)
    {
    }

    public void state_HELP(boolean init)
    {
    }

    public void state_ABOUT(boolean init)
    {
    }

    //------------------------------------------
    // FONTS
    //------------------------------------------

    //------------------------------------------
    // SOUNDS
    //------------------------------------------

    //------------------------------------------
    // ...
    //------------------------------------------

    //------------------------------------------
    // ...
    //------------------------------------------
}
